package com.example.community.util;

import com.alibaba.fastjson.JSONObject;
import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetIpAddress {

    public static String getIp(HttpServletRequest request){
//        System.out.println(request.getRemoteAddr() + ":" + getIpAddress(request.getRemoteAddr()));
        String ipAddress;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    try {
                        ipAddress = InetAddress.getLocalHost().getHostAddress();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                }
            }
            // 通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null) {
                if (ipAddress.contains(",")) {
                    return getIpAddress(ipAddress.split(",")[0]);
                } else {
                    return getIpAddress(ipAddress);
                }
            } else {
                return "未知1";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "未知2";
        }
    }

    public static String getIpAddress(String ip){
        String path = "https://ipaddquery.api.bdymkt.com/ip/query";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        request.setCredentials("9af589c25d1f4d68b88d13107cd1fd2f", "3c6df9a5232c4c03938eba53ba91074e");

        // 设置header参数
        request.addHeaderParameter("Content-Type", "application/json; charset=utf-8");

        // 设置query参数
        request.addQueryParameter("ip", ip);


        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

        try {
            ApiExplorerResponse response = client.sendRequest(request);
            JSONObject jsonObject = JSONObject.parseObject(response.getResult());
            if (jsonObject.getString("success").equals("true")){
                JSONObject data = jsonObject.getJSONObject("data");
                if (data.getString("country").equals("中国")){
                    return data.getString("region");
                }else {
                    return data.getString("country") + data.getString("region");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "未知3";
        }
        return "未知4";
    }
}
